﻿namespace Tszy.Unlimited.Base.Extensions;

/// <summary>
/// 字典扩展类。
/// </summary>
public static class DictionaryExtensions
{
    /// <summary>
    /// 获取与指定键关联的值。
    /// </summary>
    /// <typeparam name="TKey">要获取其值的键的类型。</typeparam>
    /// <typeparam name="TValue">与该键相关联的值的类型。</typeparam>
    /// <param name="currentDictionary">字典。</param>
    /// <param name="key">要获取其值的键。</param>
    /// <returns>与该键相关联的值。</returns>
    public static TValue? GetValue<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary, TKey key)
    {
        if (currentDictionary == null)
            return default;

        if (!currentDictionary.ContainsKey(key))
            return default;

        var status = currentDictionary.TryGetValue(key, out TValue? value);

        return status ? value : default;
    }
}
